import string, os, sys
import cv2

g_mid = None
g_normal = None
g_alpha = None
g_depth = None
g_data = []
#record mouse click
def on_mouse(event, x, y, flags, param):   
    if True:
        if event == cv2.EVENT_LBUTTONDOWN:            
            cv2.circle(g_mid,(int(x),int(y)),10,(0,0,255))
            cv2.imshow('image', g_mid)
            print x, y
            print g_normal[y, x]
            print g_depth[y, x]
            data = []
            data.append(x)
            data.append(y)
            data.append(g_depth[y, x])
            data.append(g_normal[y, x][0])
            data.append(g_normal[y, x][1])
            data.append(g_normal[y, x][2])            
            #print data
            g_data.append(data)
            #print g_alpha[y, x]
    
#read file names
dir = os.getcwd()
imgName = []
for root, dirs, files in os.walk(dir):
    for name in files:
        if name.endswith(".png"):
            full_name = os.path.join(root, name)
            
            if full_name.find('mid') != -1:
                g_mid = cv2.imread(name, 1)
            elif full_name.find('normal') != -1:
                g_normal = cv2.imread(name, 1)
            elif full_name.find('alpha') != -1:
                g_alpha = cv2.imread(name, 0)
            elif full_name.find('depth') != -1:
                g_depth = cv2.imread(name, 0)
            else:
                print full_name

assert g_mid != None
assert g_normal != None
assert g_alpha != None
assert g_depth != None

#read mark image
cv2.namedWindow('image')
cv2.setMouseCallback('image', on_mouse, None)

cv2.imshow('image', g_mid)

while True:
    c = cv2.waitKey()
    if c== 27:  #Esc key
        break   

cv2.destroyWindow('image')
output = open('sample_point.txt', 'w')
output.write('x y depth n_b n_g n_r\n')
output.write('[\n')
for data in g_data:   
    output.write(str(data) + ',\n')
output.write(']')
output.close()



'''

cv2.destroyWindow('image')

print("write result to file image_pv.txt.")
output = open('image_pv.txt', 'w')
for name, pt in zip(imgName, pts):
    output.write(name)
    output.write("\n")
    output.write("%d\n" %len(pt))
    for p in pt:
        output.write("%d %d %c\n" % p)
    output.write("\n")
output.close()

print("write position to file position_data.txt.")
output = open('position_data.txt', 'w')
output.write("%d\n" %(len(pts) * 2))
idx = 0
for name, pt in zip(imgName, pts):
    idx += 1
    #mid image
    output.write("%d\n" %idx)
    output.write(name+"\n")    
    output.write(name+"\n")    
    #positions
    output.write("%d\n" %len(pt))
    for p in pt:
        output.write("%d %d %c\n" %p)
    output.write("\n")

    #left and right image
    #positions
    idx += 1
    output.write("%d\n" %idx)
    left = name.replace("mid", "left")
    right = name.replace("mid", "right")
    output.write(left+"\n")
    output.write(right+"\n")
    output.write("%d\n" %len(pt))
    for p in pt:
        output.write("%d %d %c\n" %p)
    output.write("\n")
output.close()
'''    
    


            
